#include "ExampleScene.h"

#include <graphics/utils.h>
#include <graphics/image2d.h>
#include <assert.h>
#include <math.h>
#include <render/renderer.h>

ExampleScene::ExampleScene()
    : Yam3DScene()
    , m_camera(0)
    , m_renderList()
    , m_meshes()
{

    // Create camera for our scene.
    assert( m_camera == 0 );
    m_camera = new graphics::Camera();
    m_camera->setTranslation( QVector3D(0,0,4.0f) );

 /*   graphics::ShaderUniforms* uniforms = new graphics::ShaderUniforms();
    uniforms->setUniformValue("vecLightPositionWorld", m_camera->getTranslation()+QVector3D(-10,10,30) );
*/
    // Create 6 meshes and set them to corect places
    m_meshes.resize(6);

    for( int i=0;  i<3; ++i )
    {
   /*     m_passes[i] = new graphics::Pass();
        m_passes[i]->setCamera( m_camera );
        m_passes[i]->setShaderUniforms( uniforms );
     */
        QVector3D delta(2.0,0,0);

        m_meshes[i] = graphics::genSphere( 16, 0.9f );
        m_meshes[i+3] = graphics::genPlane( 2, 2, 1.8f, 1.8f );
        m_meshes[i]->setTranslation( -delta + i*delta + QVector3D(0,1.0f,0) );
        m_meshes[i+3]->setTranslation( -delta + i*delta + QVector3D(0,-1.0f,0) );
    }


    // Load effect
    graphics::EffectFile effectFile(m_camera,"C:/Users/Mikko/work/yam3d-engine/examples/5_1_Materials/BumpmappedStone.mat.xml");
    qDebug( "Num effects %d", effectFile.getEffectCount() );

   /* for( int i=0; i<effectFile.getEffectCount(); ++i )
    {
        m_effects.append( effectFile.getEffect(i) );
    }*/


    // Add meshed to render list
    m_renderList.add(effectFile.getEffect("Per vertex lighting"), m_meshes[0]);
    m_renderList.add(effectFile.getEffect("Per vertex lighting"), m_meshes[3]);
    m_renderList.add(effectFile.getEffect("Per pixel lighting"), m_meshes[1]);
    m_renderList.add(effectFile.getEffect("Per pixel lighting"), m_meshes[4]);
    m_renderList.add(effectFile.getEffect("Bump mapped"), m_meshes[2]);
    m_renderList.add(effectFile.getEffect("Bump mapped"), m_meshes[5]);
}

ExampleScene::~ExampleScene()
{
}

void ExampleScene::resize(int width, int height)
{
    m_camera->setPerspective(60.0f, float(width)/float(height), 2.0f, 10.0f);
    glViewport( 0,0, width, height);
}

void ExampleScene::update(float deltaTime)
{
    m_meshes[0]->setOrientation( m_meshes[0]->getOrientation()
        * (QQuaternion::fromAxisAndAngle(1,0,0,15*deltaTime)
        * QQuaternion::fromAxisAndAngle(0,1,0,16*deltaTime)
        * QQuaternion::fromAxisAndAngle(0,0,1,17*deltaTime)) );

    for(int i=1; i<m_meshes.size(); ++i )
    {
        m_meshes[i]->setOrientation(m_meshes[0]->getOrientation());
    }
}

void ExampleScene::render() const
{
    render::Renderer renderer;
    m_renderList.render(&renderer);
}


